# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html

import scrapy
import re
from scrapy.loader.processors import MapCompose, TakeFirst, Identity, Join
from scrapy.loader import ItemLoader


class ArticlespiderItem(scrapy.Item):
    # define the fields for your item here like:
    # name = scrapy.Field()
    name = scrapy.Field()
    src = scrapy.Field()

    pass
# 如果出现了不想要的值  可以赋予空值
def remove_tags(value):
    if value=="liunx":
        return ""
    else:
        return

def date_convert(value):
    match_re = re.match(".*?(\d+.*)", value)
    if match_re:
        return match_re.group(1)
    else:
        return "2002-01-01"


class ArticleItemLoader(ItemLoader):
    default_output_processor = TakeFirst()

    pass


class JobBoleArticleItem(scrapy.Item):
    # MapCompose通过方法修改值
    # TakeFirst将list转换为string
    title = scrapy.Field(
        # input_processor=MapCompose(add_jobbole),
        # output_processor=TakeFirst()
    )
    create_date = scrapy.Field(
        input_processor=MapCompose(date_convert)
    )
    url = scrapy.Field()
    url_object_id = scrapy.Field()
    front_image_url = scrapy.Field(
        # Identity()是默认的   不转换str
        output_processor=Identity()
    )
    front_image_path = scrapy.Field()
    praise_nums = scrapy.Field()
    comment_nums = scrapy.Field()
    fav_nums = scrapy.Field()
    tags = scrapy.Field(
        output_processor=Join(separator=",")
    )
    content = scrapy.Field()

    pass
